<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <meta content="" name="keywords">

  <title> Go - </title>
  <meta name="description" content="" />
  <meta property="og:title" content="Go" />
  <meta name="twitter:title" content="Go" />
  <meta name="author" content="{Description { .Site.Author.name }}"/>
  <meta name="twitter:card" content="summary" />
  <meta property="og:url" content="https://systemime.github.io/tags/go/" />
  <meta property="og:type" content="website" />
  <meta property="og:site_name" content="听风的小站" />

  <meta name="generator" content="Hugo 0.67.0" />
  <link rel="canonical" href="https://systemime.github.io/tags/go/" />
  <link rel="alternate" href="https://systemime.github.io/index.xml" type="application/rss+xml" title="听风的小站">
  <script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/jquery-1.12.4.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  
  
  <link rel="stylesheet" href="https://systemime.github.io/css/main.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/my.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pace.css" />
  <link rel="stylesheet" href="https://cdn.plyr.io/3.4.7/plyr.css">
  <link rel="stylesheet" href="https://systemime.github.io/css/lightgallery.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/baguetteBox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/search.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/reward.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/share.min.css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pygment_highlights.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/highlight.min.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/toc.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/lightbox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/live2d/css/live2d.css" />
  
  <link href="https://systemime.github.io/css/video-js.css" rel="stylesheet">


<meta name="baidu-site-verification" content="g8IYR9SNLF" />
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cffe439e37449bb1c07ab26ab56484bb";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<link rel="stylesheet" href="https://systemime.github.io/css/prism.css" />









<script src="https://systemime.github.io/js/pace.min.js" data-no-instant></script>



<script src="https://systemime.github.io/js/instantclick.min.js" data-no-instant></script>
   <script data-no-instant>
   InstantClick.on('change', function(isInitialLoad) {
     if (isInitialLoad === false) {
       if (typeof MathJax !== 'undefined') 
         MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
       if (typeof prettyPrint !== 'undefined') 
         prettyPrint();
       if (typeof _hmt !== 'undefined')  
         _hmt.push(['_trackPageview', location.pathname + location.search]);
       if (typeof ga !== 'undefined')  
           ga('send', 'pageview', location.pathname + location.search);
     }
   });
   InstantClick.init();
</script>




<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.7.1/gist-embed.min.js"></script>


<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-3925981084585036",
    enable_page_level_ads: true
  });
</script>



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.css" integrity="sha256-sCl5PUOGMLfFYctzDW3MtRib0ctyUvI9Qsmq2wXOeBY=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/default-skin/default-skin.min.css" integrity="sha256-BFeI1V+Vh1Rk37wswuOYn5lsTcaU96hGaI7OUVCLjPc=" crossorigin="anonymous" />



<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

<div class="pswp__bg"></div>

<div class="pswp__scroll-wrap">
    
    <div class="pswp__container">
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
    </div>
    
    <div class="pswp__ui pswp__ui--hidden">
    <div class="pswp__top-bar">
      
      <div class="pswp__counter"></div>
      <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
      <button class="pswp__button pswp__button--share" title="Share"></button>
      <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
      <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
      
      
      <div class="pswp__preloader">
        <div class="pswp__preloader__icn">
          <div class="pswp__preloader__cut">
            <div class="pswp__preloader__donut"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
      <div class="pswp__share-tooltip"></div>
    </div>
    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
    </button>
    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
    </button>
    <div class="pswp__caption">
      <div class="pswp__caption__center"></div>
    </div>
    </div>
    </div>
</div>

</head>

  <body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="https://systemime.github.io">听风的小站</a>
    </div>
    <div class="collapse navbar-collapse" id="main-navbar">
      <ul class="nav navbar-nav navbar-right">
        
          
            <li>
		    <a title="博客" href="https://systemime.github.io/blog/">博客</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="片刻" href="https://systemime.github.io/moment/">片刻</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="关于" href="https://systemime.github.io/about/">关于</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="留言" href="https://systemime.github.io/message/">留言</a>
              
              
            </li>
          
        

        

        
        
      </ul>
    </div>

    <div class="avatar-container">
      <div class="avatar-img-border">
        
      </div>
    </div>

  </div>
</nav>






    
  <header class="header-section ">
    <div class="intro-header no-img">
      <div class="container">
        <div class="row">
          <div class="col-lg-12 col-md-12 col-md-offset-0">
            <div class="page-heading">
              <h1>#Go</h1>
              <hr class="small">
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>

    
  <div class="container" role="main">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        
        <div class="posts-list">
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-05/go%E8%AF%AD%E6%B3%95%E6%95%B0%E7%BB%84/">
                <h2 class="post-title">Go语法——数组</h2>

                
              </a>

              <p class="post-meta">
                Posted on May 24, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="基本定义">基本定义</h2>
<p>你会看到集中常见的数组定义方法：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">var</span> <span class="nx">arr1</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span> <span class="c1">// 整形数组
</span><span class="c1"></span>
<span class="nx">arr2</span> <span class="o">:=</span> <span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span> <span class="c1">// 同时赋值版本
</span><span class="c1"></span>
<span class="nx">arr3</span> <span class="o">:=</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">}</span> <span class="c1">// 不定长版本
</span><span class="c1"></span>
<span class="kd">var</span> <span class="nx">grid</span> <span class="p">[</span><span class="mi">4</span><span class="p">][</span><span class="mi">5</span><span class="p">]</span><span class="kt">bool</span> <span class="c1">// 二维布尔类型版本
</span></code></pre></div><p><code>...</code>不知道你们还记得不，不定参数的意思，可以输入任意长度的参数，在这里是让编辑器自动判断数组有几个数</p>
<p>也有这样定义的</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">var</span> <span class="nx">cheeses</span> <span class="p">=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">string</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>

<span class="err">#</span> <span class="nx">make生成string类型的</span><span class="err">，</span><span class="nx">长度为2的空数组</span>
</code></pre></div><h2 id="赋值">赋值</h2>
<p>和常用的语言赋值操作没啥区别，go语言数组有很独特的切片，这种方式在后面会提到</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">var</span> <span class="nx">arr1</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span>
<span class="nx">arr1</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">=</span> <span class="mi">1</span>
</code></pre></div><h2 id="遍历">遍历</h2>
<p>学习过python的同学对内置函数<code>range</code>肯定不陌生，熟练的便能写出如下代码：</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
    <span class="o">...</span>
</code></pre></div><p>前面有提到，go语言的函数可以拥有多个返回值，这种特性在go语言的<code>range</code>函数中也不行例外，在go语言中的<code>range</code>具体用法如下</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go">	<span class="kd">var</span> <span class="nx">arr</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">}</span>
	<span class="k">for</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">arr</span> <span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="nx">v</span><span class="p">)</span>
	<span class="p">}</span>
</code></pre></div><p>在这里，range对数组arr进行遍历，同时返回两个值，<code>i</code>表示现在遍历到的数组下标，<code>v</code>表示这个下标上数组中的值，一个代表位置，一个代表具体的数</p>
<p>这样就没有了普通循环遍历中使用arr[i]来获取对应数组位置上的数值的语句</p>
<p>但例外的是，前面提到对go语言中函数多个返回值若只想要其中一个，则其他的返回值位置要使用<code>_</code>占位，</p>
<p>在这里，如果我只想要数组下标，可以直接写成：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go">	<span class="kd">var</span> <span class="nx">arr</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">}</span>
	<span class="k">for</span> <span class="nx">i</span><span class="o">:=</span> <span class="k">range</span> <span class="nx">arr</span> <span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span>
	<span class="p">}</span>
</code></pre></div><p>如果要数值则需要老老实实的把<code>i</code>替换成<code>_</code></p>
<h2 id="类型的讨论">类型的讨论</h2>
<p>对go语言，<strong>其数组是值类型的</strong></p>
<p>证明如下</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>

<span class="kd">func</span> <span class="nf">printArray</span><span class="p">(</span><span class="nx">arr</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
	<span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">=</span> <span class="mi">100</span>
	<span class="k">for</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">arr</span> <span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="nx">v</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="kd">var</span> <span class="nx">arr1</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">10</span><span class="p">}</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;printArray(arr1)&#34;</span><span class="p">)</span>
	<span class="nf">printArray</span><span class="p">(</span><span class="nx">arr1</span><span class="p">)</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">arr1</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>上述程序大家可以跑一跑，输出如下：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go">	<span class="nf">printArray</span><span class="p">(</span><span class="nx">arr1</span><span class="p">)</span>
	<span class="mi">0</span> <span class="mi">100</span>
	<span class="mi">1</span> <span class="mi">4</span>
	<span class="mi">2</span> <span class="mi">6</span>
	<span class="mi">3</span> <span class="mi">8</span>
	<span class="mi">4</span> <span class="mi">10</span>
	<span class="p">[</span><span class="mi">2</span> <span class="mi">4</span> <span class="mi">6</span> <span class="mi">8</span> <span class="mi">10</span><span class="p">]</span>
</code></pre></div><p>可以观察到，在函数printArray中arr[0]的值被修改为100，但是在main里面，arr[0]
这个位置上依旧是2</p>
<p>因为go语言在数组传递中是将数组拷贝传递过去，相当于一份副本被传递，当然，也是可以修改的，这就要使用到指针</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>

<span class="kd">func</span> <span class="nf">printArray</span><span class="p">(</span><span class="nx">arr</span> <span class="o">*</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
	<span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">=</span> <span class="mi">100</span>
	<span class="k">for</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">v</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">arr</span> <span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="nx">v</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="kd">var</span> <span class="nx">arr1</span> <span class="p">[</span><span class="mi">5</span><span class="p">]</span><span class="kt">int</span><span class="p">{</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">10</span><span class="p">}</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;printArray(arr1)&#34;</span><span class="p">)</span>
	<span class="nf">printArray</span><span class="p">(</span><span class="o">&amp;</span><span class="nx">arr1</span><span class="p">)</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">arr1</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>输出如下：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go">	<span class="nf">printArray</span><span class="p">(</span><span class="nx">arr1</span><span class="p">)</span>
	<span class="mi">0</span> <span class="mi">100</span>
	<span class="mi">1</span> <span class="mi">4</span>
	<span class="mi">2</span> <span class="mi">6</span>
	<span class="mi">3</span> <span class="mi">8</span>
	<span class="mi">4</span> <span class="mi">10</span>
	<span class="p">[</span><span class="mi">100</span> <span class="mi">4</span> <span class="mi">6</span> <span class="mi">8</span> <span class="mi">10</span><span class="p">]</span>
</code></pre></div><p>最后，总结一句，go语言中一般很少直接使用数组，除非必须使用，基本使用切片来代替，下次我们会具体介绍go语言的切片</p>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/go/">Go</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%87%BD%E6%95%B0/">函数</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-05/go%E8%AF%AD%E6%B3%95%E5%87%BD%E6%95%B0/">
                <h2 class="post-title">Go语法——函数</h2>

                
              </a>

              <p class="post-meta">
                Posted on May 16, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="一个简单函数">一个简单函数</h2>
<p>在go语言中，函数的命名方式与变量相同，都是关键字，函数名、类型这种，类型写在最后的方式</p>
<p>函数具体写法如下：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">eval</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span><span class="nx">op</span> <span class="kt">string</span><span class="p">)</span> <span class="kt">int</span><span class="p">{</span>
	<span class="o">...</span>
<span class="p">}</span>
</code></pre></div><p>下面是一个简单的函数写法及调用方法：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>
<span class="kd">func</span> <span class="nf">eval</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span> <span class="kt">int</span><span class="p">,</span> <span class="nx">op</span> <span class="kt">string</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
	<span class="k">switch</span> <span class="nx">op</span> <span class="p">{</span>
	<span class="k">case</span> <span class="s">&#34;+&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">+</span><span class="nx">b</span>
	<span class="k">case</span> <span class="s">&#34;-&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">-</span><span class="nx">b</span>
	<span class="k">case</span> <span class="s">&#34;/&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">/</span><span class="nx">b</span>
	<span class="k">case</span> <span class="s">&#34;*&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">*</span><span class="nx">b</span>
	<span class="k">default</span><span class="p">:</span>
		<span class="nb">panic</span><span class="p">(</span><span class="s">&#34;unsupported operation: &#34;</span><span class="o">+</span><span class="nx">op</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nf">eval</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="s">&#34;*&#34;</span><span class="p">))</span>
<span class="p">}</span>
</code></pre></div><h2 id="多返回值的函数">多返回值的函数</h2>
<h3 id="先看一个两个返回值的函数">先看一个两个返回值的函数</h3>
<p>在go语言中，前面也提到过，go语言支持多个返回值，现在介绍的是go语言的，两个返回值的写法：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="c1">// 两个返回值的函数
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">div</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="p">){</span> <span class="c1">// 带余数除法
</span><span class="c1"></span>	<span class="k">return</span> <span class="nx">a</span><span class="o">/</span><span class="nx">b</span><span class="p">,</span> <span class="nx">a</span><span class="o">%</span><span class="nx">b</span>
<span class="p">}</span>
</code></pre></div><p>如上，在两个返回值的函数写法中，在定义返回值类型的地方再写一个返回值的类型即可</p>
<h3 id="返回值分不清来起个名吧">返回值分不清？来起个名吧</h3>
<p>go语言支持为函数的返回值起名字，具体的写法如下：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>

<span class="c1">// 给返回值起名
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">div</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="nx">q</span><span class="p">,</span><span class="nx">r</span> <span class="kt">int</span><span class="p">){</span><span class="c1">// 带余数除法
</span><span class="c1"></span>	
	<span class="c1">//方法一：
</span><span class="c1"></span>	<span class="nx">q</span> <span class="p">=</span> <span class="nx">a</span><span class="o">/</span><span class="nx">b</span>
	<span class="nx">r</span><span class="p">=</span><span class="nx">a</span><span class="o">%</span><span class="nx">b</span>
	<span class="k">return</span>

	<span class="c1">// 方法二：
</span><span class="c1"></span>	<span class="k">return</span> <span class="nx">a</span><span class="o">/</span><span class="nx">b</span><span class="p">,</span> <span class="nx">a</span><span class="o">%</span><span class="nx">b</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
	<span class="nx">q</span><span class="p">,</span> <span class="nx">r</span> <span class="o">:=</span> <span class="nf">div</span><span class="p">(</span><span class="mi">13</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="c1">//:=定义它们
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">q</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>上述代码可以看出对返回值起名时遵循变量定义时，<strong>变量在前，类型在后</strong>的方式，在函数体中存在这么两种写法</p>
<p>方法一指定两个返回值q,r分别代表什么，然后返回；</p>
<p>方法二不指定具体代表，按照默认先后顺序进行返回，在主函数体内选取变量进行接受；函数返回值q，r与主函数内接收的q，r没有关联，变量名随意取，<strong>不要求相同</strong></p>
<blockquote>
<p>注意上述代码返回值代码内起名这种方式仅仅用于简单函数，复杂函数会增加代码复杂度，对调用者而言没有区别</p>
</blockquote>
<h2 id="函数返回错误">函数返回错误</h2>
<p>在其他语言中函数执行报错时会中断整个程序，而go语言可以自定义返回错误信息，并借助多返回值的优势，帮助使用者调试代码</p>
<p>修改一下上次说到的四则运算代码，示例：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
	<span class="s">&#34;math&#34;</span>
	<span class="s">&#34;reflect&#34;</span>
	<span class="s">&#34;runtime&#34;</span>
<span class="p">)</span>
<span class="kd">func</span> <span class="nf">div</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="nx">q</span><span class="p">,</span><span class="nx">r</span> <span class="kt">int</span><span class="p">){</span>
	<span class="k">return</span> <span class="nx">a</span><span class="o">/</span><span class="nx">b</span><span class="p">,</span> <span class="nx">a</span><span class="o">%</span><span class="nx">b</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">eval2</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span> <span class="kt">int</span><span class="p">,</span> <span class="nx">op</span> <span class="kt">string</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span>
	<span class="k">switch</span> <span class="nx">op</span> <span class="p">{</span>
	<span class="k">case</span> <span class="s">&#34;+&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">+</span><span class="nx">b</span><span class="p">,</span><span class="kc">nil</span>
	<span class="k">case</span> <span class="s">&#34;-&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">-</span><span class="nx">b</span><span class="p">,</span><span class="kc">nil</span>
	<span class="k">case</span> <span class="s">&#34;*&#34;</span><span class="p">:</span>
		<span class="k">return</span> <span class="nx">a</span><span class="o">*</span><span class="nx">b</span><span class="p">,</span><span class="kc">nil</span>
	<span class="k">case</span> <span class="s">&#34;/&#34;</span><span class="p">:</span>
		<span class="c1">//多返回值使用‘_’表示接受不使用
</span><span class="c1"></span>		<span class="nx">q</span><span class="p">,</span> <span class="nx">_</span> <span class="o">:=</span> <span class="nf">div</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span>
		<span class="k">return</span> <span class="nx">q</span><span class="p">,</span><span class="kc">nil</span>
	<span class="k">default</span><span class="p">:</span>
		<span class="c1">// panic(&#34;unsupported operation: &#34;+op)
</span><span class="c1"></span>		<span class="k">return</span> <span class="mi">0</span><span class="p">,</span><span class="nx">fmt</span><span class="p">.</span><span class="nf">Errorf</span><span class="p">(</span><span class="s">&#34;unsupported operation: %s&#34;</span><span class="p">,</span> <span class="nx">op</span><span class="p">)</span>
		<span class="c1">// 自定义错误信息输出
</span><span class="c1"></span>	<span class="p">}</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
	<span class="k">if</span> <span class="nx">result</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nf">eval2</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="s">&#34;/&#34;</span><span class="p">);</span><span class="nx">err</span><span class="o">==</span><span class="kc">nil</span><span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">result</span><span class="p">)</span>
	<span class="p">}</span><span class="k">else</span><span class="p">{</span>
		<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;Error: &#34;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
	<span class="p">}</span>
<span class="p">}</span>
</code></pre></div><p>上述代码中，函数拥有两个返回值<code>int</code>类型和<code>erroe</code>类型，由于go语言中定义的变量必须使用，所以在每个返回值后面，再多返回一个nil表示error类型返回值，且无错误；</p>
<p>在主函数的<code>if</code>中调用<code>eval2</code>函数，按照switch逻辑，将调用div函数进行运算，并返回两个返回值，查看代码我们可以发现div也是两个返回值，如果我们只想要其中的一个返回值的时候应该怎么办呢？</p>
<p>类似与下面一句代码，当我们不想要某个返回值时只需要使用<code>_</code>便可以表示接收不使用</p>
<blockquote>
<p>q, _ := div(a,b)</p>
</blockquote>
<p>回到刚刚的话题，刚刚定于的<code>error</code>类型如何使用呢？从代码来看，如果我们输入参数<code>eval2(3, 4, &quot;z&quot;)</code>，很明显，我们定义的<code>eval2</code>函数没法处理<code>z</code>这个参数，这时候就会触发<code>default</code>中定义的error处理</p>
<p><code>default</code>中的return首先将int类型返回值返回一个0，然后使用<code>fmt.Errorf</code>错误信息输出语句输出错误参数并返回到主程序中并使用<code>err</code>接收，if判断err不为nil，则输出正常运行结果，否则运行自定义错误信息</p>
<h2 id="go函数式编程语言">go——函数式编程语言</h2>
<p>go语言相交之C++等其他语言，没有默认参数、可选参数、函数重载等花哨的用法</p>
<p>仅有<code>可变参数列表</code>这一种用法，关于这两点我们通过两段代码验证一下</p>
<h3 id="参数可以为函数">参数可以为函数</h3>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
	<span class="s">&#34;math&#34;</span>
	<span class="s">&#34;reflect&#34;</span>
	<span class="s">&#34;runtime&#34;</span>
<span class="p">)</span>

<span class="c1">// go语言是一个函数式编程语言，体现如下，函数内的函数
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">apply</span><span class="p">(</span><span class="nx">op</span> <span class="kd">func</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span><span class="p">,</span> <span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
	<span class="nx">p</span> <span class="o">:=</span> <span class="nx">reflect</span><span class="p">.</span><span class="nf">ValueOf</span><span class="p">(</span><span class="nx">op</span><span class="p">).</span><span class="nf">Pointer</span><span class="p">()</span>
	<span class="nx">opName</span> <span class="o">:=</span> <span class="nx">runtime</span><span class="p">.</span><span class="nf">FuncForPC</span><span class="p">(</span><span class="nx">p</span><span class="p">).</span><span class="nf">Name</span><span class="p">()</span>
	<span class="c1">// 获得函数名
</span><span class="c1"></span>	
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;函数名是: %s&#34;</span> <span class="o">+</span> <span class="s">&#34;(%d, %d)\n&#34;</span><span class="p">,</span> <span class="nx">opName</span><span class="p">,</span> <span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span>
	<span class="k">return</span> <span class="nf">op</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">Pow</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span><span class="p">{</span>
	<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nf">Pow</span><span class="p">(</span><span class="nb">float64</span><span class="p">(</span><span class="nx">a</span><span class="p">),</span> <span class="nb">float64</span><span class="p">(</span><span class="nx">b</span><span class="p">)))</span>
<span class="p">}</span>
<span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>

	<span class="c1">//fmt.Println(apply(Pow, 3 , 4))
</span><span class="c1"></span>	<span class="c1">// 函数直接写在调用处，匿名函数，无需写函数名
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nf">apply</span><span class="p">(</span>
		<span class="kd">func</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span><span class="p">{</span>
			<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nf">Pow</span><span class="p">(</span><span class="nb">float64</span><span class="p">(</span><span class="nx">a</span><span class="p">),</span> <span class="nb">float64</span><span class="p">(</span><span class="nx">b</span><span class="p">)))</span>
		<span class="p">},</span><span class="mi">3</span> <span class="p">,</span> <span class="mi">4</span><span class="p">))</span>

	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nf">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">))</span>
<span class="p">}</span>
</code></pre></div><p>如上代码，观察函数<code>apply</code>，包含三个参数和一个返回值，其中，第一个参数是一个函数，这个函数拥有两个参数和一个返回值</p>
<p>这三个参数在函数D调用时，以D(A,B,C)的形式调用</p>
<blockquote>
<p>apply(Pow, 3 , 4)</p>
</blockquote>
<p>这句话便是一个举例用法，Pow为传入的函数名，3，4是其他参数，首先apply获取到传入并运行的实际的函数名称（此处是Pow），然后输出函数名和传入的另外两个参数，在return处调用参数函数op，即Pow</p>
<p>Pow的参数便是a，b</p>
<p><strong>实际上main中调用部分传入的Pow甚至不需要提前定义</strong></p>
<p>可以写成如下这样</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="c1">// 函数直接写在调用处，匿名函数，无需写函数名
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nf">apply</span><span class="p">(</span>
		<span class="kd">func</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span><span class="p">{</span>
			<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nf">Pow</span><span class="p">(</span><span class="nb">float64</span><span class="p">(</span><span class="nx">a</span><span class="p">),</span> <span class="nb">float64</span><span class="p">(</span><span class="nx">b</span><span class="p">)))</span>
		<span class="p">},</span><span class="mi">3</span> <span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
</code></pre></div><p>采用匿名函数的方式，直接将函数写在调用处，此方法不需要写传入的函数名</p>
<h3 id="可变参数列表">可变参数列表</h3>
<p>关于这部分比较简单，只需要记住<code>...</code>即可，在go语言中<code>...</code>代表无限制参数数量</p>
<p>但是传递参数的类型必须与函数签名指定的类型相同</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>

<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
	<span class="s">&#34;math&#34;</span>
	<span class="s">&#34;reflect&#34;</span>
	<span class="s">&#34;runtime&#34;</span>
<span class="p">)</span>

<span class="kd">func</span> <span class="nf">sum</span><span class="p">(</span><span class="nx">numbers</span> <span class="o">...</span><span class="kt">int</span><span class="p">)</span> <span class="kt">int</span><span class="p">{</span>
	<span class="c1">//...代表任意数量参数
</span><span class="c1"></span>	<span class="nx">s</span><span class="o">:=</span><span class="mi">0</span>
	<span class="k">for</span> <span class="nx">i</span><span class="o">:=</span><span class="k">range</span> <span class="nx">numbers</span><span class="p">{</span>
		<span class="nx">s</span><span class="o">+=</span><span class="nx">numbers</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
	<span class="p">}</span>
	<span class="k">return</span> <span class="nx">s</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">main</span><span class="p">(){</span>
	
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nf">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">))</span>
<span class="p">}</span>
</code></pre></div><p>其中，<code>numbers</code>是一个包含所有参数的切片</p>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/go/">Go</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%87%BD%E6%95%B0/">函数</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-05/go%E5%9F%BA%E7%A1%80-%E7%A8%8B%E5%BA%8F%E5%AE%9E%E4%BD%93%E8%A1%A5%E5%85%85/">
                <h2 class="post-title">Go基础---程序实体补充</h2>

                
              </a>

              <p class="post-meta">
                Posted on May 15, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="一如何判断变量类型">一、如何判断变量类型</h2>
<p>断言</p>
<p>下面是例子代码：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>
<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">name</span> <span class="p">=</span> <span class="s">&#34;icode&#34;</span>
    <span class="nx">value</span><span class="p">,</span> <span class="nx">ok</span> <span class="o">:=</span> <span class="kd">interface</span><span class="p">{}(</span><span class="nx">name</span><span class="p">).(</span><span class="kt">string</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">ok</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p><strong>注意：类型断言的时候我们必须先转换为接口类型</strong></p>
<ul>
<li>
<p>在Go语言中interace{} 代表空接口，任何类型都是它的实现类型</p>
</li>
<li>
<p>一对不包含任何东西的花括号，除了可以代表空的代码块之外，还可以用于表示不包含任何内容的数据结构（或者说数据类型）</p>
</li>
<li>
<p>如：<code>struct{}</code>，它就代表了不包含任何字段和方法的、空的结构体类型</p>
</li>
<li>
<p>空接口<code>interface{}</code>则代表了不包含任何方法定义的、空接口类型</p>
</li>
<li>
<p>字面量：用来表示数据类型本身的若干个字符</p>
</li>
</ul>
<h2 id="二关于string类型和各种切片类型的互转">二、关于string类型和各种切片类型的互转</h2>
<ul>
<li>
<p>一个值从string类型向<code>[]byte</code>类型转换时代表<code>UTF-8</code>编码的字符串被拆分成独立的字节</p>
</li>
<li>
<p>一个值从string类型向<code>[]rune </code>类型转换的时候代表着字符串会被拆分成一个个<code>Unicode</code>字符</p>
</li>
</ul>
<h2 id="三别名类型vs-潜在类型">三、别名类型VS 潜在类型</h2>
<p>我们可以通过下面方式声明：</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">type</span> <span class="nx">MyString</span> <span class="p">=</span> <span class="kt">string</span>
</code></pre></div><p>这样<code>MyString</code> 就是<code>string</code>类型的别名类型</p>
<p>在Go内建的基本类型中就存在两个别名类型：byte是uint8的别名类型，而rune是int32的别名类型</p>
<p>而下面这种声明方式：</p>
<pre><code>type MyString string
</code></pre><p>这里MyString和string就不是一种类型了，这种方式叫做对类型的再定义</p>
<p>我们可以把string是MyString的潜在类型</p>
<p>潜在类型的含义：某个类型在本质上是哪个类型，或者是哪个类型的集合。</p>
<p>如果两个值的潜在类型相同，却属于不同的类型，它们之间是可以做类型转换的。但是它们的值不能进行判断等或比较，它们变量之间不能赋值</p>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/go/">Go</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E8%A1%A5%E5%85%85/">补充</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-05/go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/">
                <h2 class="post-title">Go语言基础语法</h2>

                
              </a>

              <p class="post-meta">
                Posted on May 15, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="hello-world">Hello world</h2>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="c1">// 程序入口
</span><span class="c1"></span><span class="kn">import</span> <span class="s">&#34;fmt&#34;</span>
<span class="c1">// 输出库
</span><span class="c1"></span><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// 主程序入口
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Print</span><span class="p">(</span><span class="s">&#34;hello world&#34;</span><span class="p">)</span>
	<span class="c1">// 输出语句
</span><span class="c1"></span><span class="p">}</span>
</code></pre></div><h2 id="变量">变量</h2>
<p><strong>在go语言中，凡是定义的变量必须使用，否则会报错</strong></p>
<h3 id="变量定义方法">变量定义方法</h3>
<blockquote>
<p>var a int</p>
</blockquote>
<p>在go语言中，一般定义变量使用关键字<code>var</code>，在go语言中，变量名写在前，变量类型在后</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">variable</span><span class="p">()</span>  <span class="p">{</span> <span class="c1">// go语言函数写法，在后面会说明，此处先看内容
</span><span class="c1"></span>	<span class="kd">var</span> <span class="nx">a</span> <span class="kt">int</span>
	<span class="kd">var</span> <span class="nx">s</span> <span class="kt">string</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;variable()输出：%d %q\n&#34;</span><span class="p">,</span><span class="nx">a</span><span class="p">,</span><span class="nx">s</span><span class="p">)</span>
	<span class="c1">// %q可以输出空字符串
</span><span class="c1"></span>	<span class="p">}</span>
</code></pre></div><p>在go语言中s是空字符串，而不是<code>null</code>，这在go语言中也是不存在<code>null</code>的，是<code>nil</code></p>
<h3 id="变量赋值">变量赋值</h3>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">variableInitialValue</span><span class="p">(){</span>
	<span class="kd">var</span> <span class="nx">a</span><span class="p">,</span><span class="nx">b</span> <span class="kt">int</span> <span class="p">=</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span>
	<span class="kd">var</span> <span class="nx">s</span> <span class="kt">string</span> <span class="p">=</span> <span class="s">&#34;abc&#34;</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;%d %q %d\n&#34;</span><span class="p">,</span><span class="nx">a</span><span class="p">,</span><span class="nx">s</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span>
	<span class="c1">// 直接输出
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">s</span><span class="p">,</span><span class="nx">b</span><span class="p">)</span>
	<span class="c1">// 带换行的输出
</span><span class="c1"></span><span class="p">}</span>
</code></pre></div><p>go语言中可以直接赋多个值，在多个变量时，也可以省去书写多个var的麻烦，如:</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">var</span> <span class="p">(</span><span class="c1">//省去多次书写var的麻烦
</span><span class="c1"></span>	<span class="nx">aaa</span><span class="p">=</span><span class="mi">3</span>
	<span class="nx">sss</span><span class="p">=</span><span class="s">&#34;kkk&#34;</span>
	<span class="nx">bb</span><span class="p">=</span><span class="kc">true</span>
	<span class="c1">// bool类型
</span><span class="c1"></span><span class="p">)</span>
</code></pre></div><p>当然，你也可以让go语言<strong>自动识别变量类型</strong>，并同时赋值多个变量</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">variableType</span><span class="p">(){</span>
	<span class="kd">var</span> <span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">,</span><span class="nx">c</span><span class="p">,</span><span class="nx">d</span> <span class="p">=</span> <span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="kc">true</span><span class="p">,</span><span class="s">&#34;def&#34;</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">,</span><span class="nx">c</span><span class="p">,</span><span class="nx">d</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>如果你连var都不想写了，当然也是可以的</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">variableShort</span><span class="p">(){</span>
	<span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">,</span><span class="nx">c</span><span class="p">,</span><span class="nx">d</span> <span class="o">:=</span> <span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="kc">true</span><span class="p">,</span><span class="s">&#34;def&#34;</span>
	<span class="c1">//e:=5 //再使用会报错,只能第一次使用
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">,</span><span class="nx">c</span><span class="p">,</span><span class="nx">d</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>但是要注意，在一段代码中<code>:=</code>的赋值方式只能使用一次，如果再使用，则会报错</p>
<p>还有一点要注意的是，在定义全局变量时（实际上go语言没有全局变量说法，只有<code>包变量</code>）</p>
<p>同时，go语言在定义全局变量时，不允许使用<code>:=</code>的方式进行赋值</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>
<span class="c1">//需要导入多个库写法
</span><span class="c1"></span>
<span class="kd">var</span> <span class="nx">aa</span> <span class="p">=</span> <span class="mi">3</span>
<span class="kd">var</span> <span class="nx">ss</span><span class="p">=</span><span class="s">&#34;kkk&#34;</span>
<span class="c1">// 没有全局变量说法，只是包变量
</span><span class="c1">// bb:=true
</span><span class="c1">// 不能在全局用:=，必须用var
</span><span class="c1"></span>
<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">aa</span><span class="p">,</span> <span class="nx">ss</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><p>go语言与其他语言不同的地方在于它<strong>支持复数</strong>，具体使用方法见代码注释</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kn">package</span> <span class="nx">main</span>
<span class="kn">import</span> <span class="p">(</span>
	<span class="s">&#34;fmt&#34;</span>
	<span class="s">&#34;math/cmplx&#34;</span>
<span class="p">)</span>
<span class="kd">func</span> <span class="nf">euler</span><span class="p">(){</span>
	<span class="nx">c</span><span class="o">:=</span><span class="mi">3</span><span class="o">+</span><span class="m">4i</span> <span class="c1">//复数类型不能用4*i，会误以为i是变量
</span><span class="c1"></span>	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">cmplx</span><span class="p">.</span><span class="nf">Abs</span><span class="p">(</span><span class="nx">c</span><span class="p">),)</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="s">&#34;%.3f\n&#34;</span><span class="p">,</span><span class="nx">cmplx</span><span class="p">.</span><span class="nf">Exp</span><span class="p">(</span><span class="m">1i</span> <span class="o">*</span> <span class="nx">math</span><span class="p">.</span><span class="nx">Pi</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="c1">//1i为了让系统知道i是个复数，不是变量
</span><span class="c1"></span><span class="p">}</span>
<span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
	<span class="nf">euler</span><span class="p">()</span>
<span class="p">}</span>

<span class="cm">/* -----------输出-------------- */</span>

    <span class="mi">5</span>
    <span class="p">(</span><span class="mf">0.000</span><span class="o">+</span><span class="m">0.000i</span><span class="p">)</span>

<span class="cm">/* -----------输出-------------- */</span>
</code></pre></div><p>交换变量时，go可以像python一样，直接将两个变量交换</p>
<pre><code>a, b = b, a
</code></pre><h3 id="常量与类型转换">常量与类型转换</h3>
<p>在go语言中，任何涉及的类型转换的地方都<strong>必须使用显示类型转换</strong></p>
<pre><code>func triangle(){
	a,b:=3,4
	var c int //go语言只能强制类型转换，没有隐式的，所以int（）之类不能省
	c = int(math.Sqrt(float64(a*a+b*b)))
	fmt.Println(c)
	//由于float有时候不准，所以这条式子有可能是4
	//该问题存在于任何编程语言中
}
</code></pre><p>以求勾股定理代码为例，<code>math.Sqrt()</code>要求以float64为输入类型，而求得的c要求以整形</p>
<p>有这么一种情况可能在实际编写的过程中被认为存在类型转换，实际上并没有，也不是所谓的隐式类型转换</p>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="kd">func</span> <span class="nf">consts</span><span class="p">(){</span>
	<span class="kd">const</span><span class="p">(</span>
		<span class="nx">filename</span> <span class="kt">string</span> <span class="p">=</span> <span class="s">&#34;abc.txt&#34;</span>
		<span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">=</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span>
	<span class="p">)</span>
	<span class="c1">// go语言中常量的写法
</span><span class="c1"></span>	<span class="cm">/*关于常量，go语言中常量书写方式一般不大写，因为在go中首字母大写有其他含义日后学习中再总结*/</span>
	
	<span class="kd">var</span> <span class="nx">c</span> <span class="kt">int</span>
	<span class="nx">c</span> <span class="p">=</span> <span class="nb">int</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nf">Sqrt</span><span class="p">(</span><span class="nx">a</span><span class="o">*</span><span class="nx">a</span><span class="o">+</span><span class="nx">b</span><span class="o">*</span><span class="nx">b</span><span class="p">))</span>
	
	<span class="c1">//上面定义未指定a，b类型，可以是int也可以是float
</span><span class="c1"></span>	<span class="c1">// 所以在上面不需要显示转换，被认为是float64
</span><span class="c1"></span>	
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">filename</span><span class="p">,</span><span class="nx">c</span><span class="p">)</span>
<span class="p">}</span>

<span class="cm">/* -----------输出-------------- */</span>

    <span class="nx">abc</span><span class="p">.</span><span class="nx">txt</span> <span class="mi">5</span>

<span class="cm">/* -----------输出-------------- */</span>
	
</code></pre></div><h2 id="if与switch">if与switch</h2>
<p>在go语言中，它的if与switch写法与其他语言在一般的写法上没有什么太大区别，增加了一些特色的写法，如可以在if后面执行一条语句，</p>
<h3 id="if写法">if写法</h3>
<p>以这段代码为例子，代码中的<code>nil</code>可以看这篇文章<a href="https://qfdxz.top/2019/02/11/Golang%E4%B9%8Bnil/">Golang之nil</a>讲解相对详细，在此不废话</p>
<pre><code>package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
)
func iftext(){
	const filename = &quot;abc.txt&quot;
	// windows有时可以使用绝对路径如：F:\\GO_S\\stury001\\src\\abc.txt
	
	contents, err := ioutil.ReadFile(filename)
	/* go返回值有两个
	   第一个返回值是正常的返回值
	   第二个返回值err通常作为程序状态返回信息 */
	/* 该方法时go语言读取文件内容，如果文件不存在时err获取到错误信息 */
	/* 文件存在时读取文件内容 */
	if err != nil {
		fmt.Println(err)
	}else{
		fmt.Printf(&quot;%s\n&quot;,contents)
	}
	
	//特色写法
	if contents2, err2 := ioutil.ReadFile(filename);err2 !=nil {
		fmt.Println(err)
	}else{
		fmt.Printf(&quot;%s\n&quot;,contents2)
	}
	//fmt.Println(contents2)
	// 这句话会报错，因为contents2生存期只在特色写法的if中存在
}

func iftext2(){
	if a:= 1==1; a{
		fmt.Println(&quot;1==1&quot;)
	}
}

func main() {
	fmt.Println()
	iftext()
	iftext2()
}

/* -----------输出-------------- */

    文件不存在时：open abc.txt: The system cannot find the file specified.
	
	文件存在时：文件内容

/* -----------输出-------------- */
</code></pre><p><strong>注意</strong></p>
<ul>
<li>if判断条件没有括号</li>
<li>if语句后面必须有<code>{}</code>，无论下面几句话</li>
<li>else必须<strong>紧跟</strong>在if的{}后</li>
<li>else后面也必须有<code>{}</code>,无论下面几句话</li>
</ul>
<h3 id="switch写法">switch写法</h3>
<pre><code>package main

import (
	&quot;fmt&quot;
)

func grade(score int) string {
	g :=&quot;&quot;
	switch {
	case score&lt;60:
		g=&quot;F&quot;
	case score&lt;80:
		g=&quot;C&quot;
	case score&lt;90:
		g=&quot;B&quot;
	case score&lt;=100:
		g=&quot;A&quot;
	default:
		panic(fmt.Sprintf(&quot;Wrong score:%d&quot;,score))
	}
	return g
}

func grade2(){
	for i:=0;i&lt;5;i++{
		switch i{
		//此处i可以做运算，如i%2==0
			case 0, 2:
				fmt.Println(&quot;Even&quot;)
			case 0, 2:
				fmt.Println(&quot;0dd&quot;)
			defaule:
				fmt.Println(&quot;it is not &quot;)
		}
	}
}

func main() {
	fmt.Println()
	fmt.Println(
		grade(0),
		grade(59),
		grade(60),
		grade(82),
		grade(100),
		)//若输入错误，整个程序中断，
		// 输出default中内容，同时不显示其他参数执行结果
}

/* -----------输出-------------- */

    F F C B A

/* -----------输出-------------- */
</code></pre><h2 id="枚举">枚举</h2>
<p>这部分直接看样例代码与输出</p>
<pre><code>package main
//day2  常量与枚举
import (
	&quot;fmt&quot;
	&quot;math&quot;
)
const filename1 = &quot;123.txt&quot;//常量也可以在包内定义

func enums1()  {//go没有特殊枚举关键字，一般用一组const
	const( //const必须给其赋值
		cpp = 0
		java = 1
		python = 3
		golang = 5
	)
	const(//const高级用法
		b = 1&lt;&lt;(10*iota)  //移位，下面变量全部使用该公式，结果递增
		kb
		mb
		gb
		tb
		pb
	)
	fmt.Println(cpp,java,python,golang)
	fmt.Println(b,kb,mb,gb,tb,pb)
}
func enums2()  {
	const( //const自增属性
		cpp = iota //若直接cpp=1，则const内所有变量均为1
		//自增顺序以const组中变量书写顺序为准（0开始）
		java
		python
		golang
	)
	fmt.Println(cpp,java,python,golang)
}
func main() {
	fmt.Println(filename1)//输出全局常量
	enums1()
	enums2()
}

/* -----------输出-------------- */

    123.txt
	
	enums1()输出：
	0 1 3 5
	1 1024 1048576 1073741824 1099511627776 1125899906842624
	
	enums2()输出：
	0 1 2 3

/* -----------输出-------------- */
</code></pre><h2 id="循环">循环</h2>
<p>在go语言中，仅支持循环关键字<code>for</code>, 没有<code>while</code>循环，<code>while</code>被<code>for</code>的不同写法代替</p>
<ul>
<li>条件循环</li>
</ul>
<div class="highlight"><pre class="chroma"><code class="language-go" data-lang="go"><span class="nx">n</span> <span class="o">:=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="nx">n</span><span class="p">&lt;</span><span class="mi">5</span><span class="p">{</span>
	<span class="nx">n</span><span class="o">++</span>
	<span class="nx">fmt</span><span class="p">.</span><span class="nf">Printf</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div><ul>
<li>无限循环</li>
</ul>
<pre><code>n:=0
for {
	...
}
</code></pre><ul>
<li>for循环</li>
</ul>
<pre><code>for(j:=7; j&lt;=9 ;j++){
	...
}
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/go/">Go</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%9F%BA%E7%A1%80/">基础</a>&nbsp;
                  
                </span>
              

            </article>
          
        </div>
        
          <ul class="pager main-pager">
            
            
              <li class="next">
                <a href="https://systemime.github.io/tags/go/page/2">Older Posts &rarr;</a>
              </li>
            
          </ul>
        
      </div>
    <div align="center" class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        

<ul class="pagination">
    
    <li class="page-item">
        <a href="https://systemime.github.io/tags/go/" class="page-link" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
    </li>
    
    <li class="page-item disabled">
    <a  class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
    </li>
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item active"><a class="page-link" href="https://systemime.github.io/tags/go/">1</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/tags/go/page/2/">2</a></li>
    
    
    <li class="page-item">
    <a href="https://systemime.github.io/tags/go/page/2/" class="page-link" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
    </li>
    
    <li class="page-item">
        <a href="https://systemime.github.io/tags/go/page/2/" class="page-link" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
    </li>
    
</ul>


        
        
        
        
        
      </div>
    </div>
  </div>

    <div class="rocket"><img src="https://systemime.github.io/img/rocket_up.png" alt="" width="100" height="100"></div>
<script>
    $(function () {
      $(window).scroll(function () {
        
        if ($(window).scrollTop() >= 1000) {
          $('.rocket').stop().fadeIn(1000);
        }else {
          $('.rocket').stop().fadeOut(1000);
        }
      })
      
      $('.rocket').click(function () {
        $('html,body').stop().animate({scrollTop:0},400);
       
      })
    })

</script>

    
    <div id="landlord">
        <div class="message" style="opacity:0"></div>
        <canvas id="live2d" width="280" height="250" class="live2d"></canvas>
        <div class="hide-button">隐藏</div>
    </div>
    
    <footer id="copyright">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center footer-links">
          
          
        </ul>
        <p class="credits copyright text-muted">
        &copy;2017-2018
          
          &nbsp;&bull;&nbsp;
          September 20,2019
          updated
          
            &nbsp;&bull;&nbsp;
            <a href="https://systemime.github.io">Home</a>
          
        </p>

        <p class="credits theme-by text-muted">
        <a href="https://servicemesher.github.io" target="_blank">ServiceMesher</a>&nbsp;&bull;&nbsp;<a href="https://github.com/yangchuansheng/k8s-knowledge" target="_blank">Kubernetes 知识图谱</a>
        <br />
        <span id="busuanzi_container_site_pv">
            本站已被访问 <span id="busuanzi_value_site_pv"></span> 次啦
        </span>
        <span id="busuanzi_container_site_uv">
            &ensp;|&ensp;&thinsp;您是第 <span id="busuanzi_value_site_uv"></span> 位到访的小伙伴喔
        </span>
        </p>
        
        <p class="credits theme-by text-muted">
          <a href="http://gohugo.io">Hugo v0.67.0</a> powered &nbsp;&bull;&nbsp; Theme by <a href="http://deanattali.com/beautiful-jekyll/">Beautiful Jekyll</a> adapted to <a href="https://github.com/halogenica/beautifulhugo">Beautiful Hugo</a>
          
          <center><section class="credits theme-by text-muted">
    <span class="footer__copyright">
    <div>
    <span id="span_dt_dt"> </span>
    <script language="javascript">
      function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay=new Date("7/8/2017 10:56:12");
        today=new Date();
        timeold=(today.getTime()-BirthDay.getTime());
        sectimeold=timeold/1000
        secondsold=Math.floor(sectimeold);
        msPerDay=24*60*60*1000
        e_daysold=timeold/msPerDay
        daysold=Math.floor(e_daysold);
        e_hrsold=(e_daysold-daysold)*24;
        hrsold=Math.floor(e_hrsold);
        e_minsold=(e_hrsold-hrsold)*60;
        minsold=Math.floor((e_hrsold-hrsold)*60);
        seconds=Math.floor((e_minsold-minsold)*60);
        span_dt_dt.innerHTML="本博客已经开心运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
      }
      show_date_time();
    </script>
    </div>
</script>
</section>
</center>
        </p>
      </div>
    </div>
  </div>
</footer>


<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/bootstrap.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe-ui-default.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/auto-render.min.js"></script>
<script src="https://systemime.github.io/js/main.js"></script>
<script src="https://systemime.github.io/js/clipboard.min.js"></script>
<script src="https://systemime.github.io/js/prism.js?t=123"></script>
<script src="https://systemime.github.io/js/highlight.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/katex.min.js"></script>
<script src="https://systemime.github.io/js/reward.js"></script>
<script src="https://systemime.github.io/js/canvas_ribbon.js"></script>
<script> renderMathInElement(document.body); </script>

<script src="https://systemime.github.io/js/baguetteBox.js"></script>
<script> baguetteBox.run('.gallery');</script>






<script async defer src="https://buttons.github.io/buttons.js"></script>


<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
<script type="text/javascript" src="https://systemime.github.io/js/quicklink.umd.js"></script>
<script>
window.addEventListener('load', () =>{
   quicklink();
});
</script>



<script type="text/javascript">
 
var a_idx = 0;
jQuery(document).ready(function($) {
    $("body").click(function(e) {
        var a = new Array("Docker", "Kubernetes", "Prometheus", "Envoy", "Istio", "Service Mesh", "Cloud Native");
        var $i = $("<span />").text(a[a_idx]);
        a_idx = (a_idx + 1) % a.length;
        var x = e.pageX,
        y = e.pageY;
        function randomColor() {
          var flakeColor = new Array("#FFDA65", "#00BFFF", "#BA55D3", "#FFA07A", "#87CEEB", "#FFB6C1");
          var snowColor = flakeColor[Math.floor(flakeColor.length * Math.random())];
          return snowColor;
        }
        $i.css({
            "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": randomColor()
        });
        $("body").append($i);
        $i.animate({
            "top": y - 180,
            "opacity": 0
        },
        1500,
        function() {
            $i.remove();
        });
    });
});
</script>


<script type="text/javascript" src="https://systemime.github.io/js/lightbox.js"></script>











<script src="https://cdn.plyr.io/3.4.7/plyr.js"></script>
<script>const player = new Plyr('#player');</script>


<script async defer src="https://buttons.github.io/buttons.js"></script>


  </body>
</html>

